package com.vlin;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.junit.Test;

import com.vlin.api.client.ApiClient;
import com.vlin.api.client.Client;
import com.vlin.api.client.Method;
import com.vlin.api.client.Params;
import com.vlin.api.client.defaut.DefaultClient;
import com.vlin.api.client.defaut.DefaultedApiClient;
import com.vlin.codec.HMACSHA256;
import com.vlin.constant.API;
import com.vlin.constant.TuyaAPIConstant;

public class VlinService {

	public static final String CLIENT_ID = "kd7w58t5s9c98txeg5jf";
	
	public static final String SECRET = "q5h3q9fuvmusd4u9j3uev7hnurj559fx";
	
	public static final String tuyaHost= "https://openapi.tuyacn.com/";
	
	public static final String SIGN_METHOD = "HMAC-SHA256";
	
	
	@Test
	public void doGetTestOne() {
		// 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
		CloseableHttpClient httpClient = HttpClientBuilder.create().build();
		// 创建Get请求https://github.com/
		HttpGet httpGet = new HttpGet(tuyaHost + "/v1.0/token?grant_type=1");
		long t = System.currentTimeMillis();
		String sign = HMACSHA256.sha256_HMAC(CLIENT_ID + t, SECRET).toUpperCase();
		httpGet.addHeader("client_id", CLIENT_ID);
		httpGet.addHeader("sign", sign);
		httpGet.addHeader("sign_method", SIGN_METHOD);
		httpGet.addHeader("t", String.valueOf(t));
		//httpGet.setParams(params);
		// 响应模型
		CloseableHttpResponse response = null;
		try {
			// 由客户端执行(发送)Get请求
			response = httpClient.execute(httpGet);
			// 从响应模型中获取响应实体
			HttpEntity responseEntity = response.getEntity();
			
			System.out.println("响应状态为:" + response.getStatusLine());
			if (responseEntity != null) {
				System.out.println("响应内容长度为:" + responseEntity.getContentLength());
				System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				// 释放资源
				if (httpClient != null) {
					httpClient.close();
				}
				if (response != null) {
					response.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	@Test
	public void tst() {
		long t = System.currentTimeMillis();
		String sign = HMACSHA256.sha256_HMAC(CLIENT_ID + t, SECRET).toUpperCase();
		ApiClient client = new DefaultedApiClient();
		client.setHost(tuyaHost);
		Params params = new Params();
		params.setHeadParam("client_id", CLIENT_ID);
		params.setHeadParam("sign", sign);
		params.setHeadParam("sign_method", SIGN_METHOD);
		params.setHeadParam("t", String.valueOf(t));
		params.setParam("grant_type", "1");
		JSONObject result = client.send("/v1.0/token", params, Method.GET);
		System.out.println(result);
	}
	
	@Test
	public void searchUserDevices() {
		Client client = new DefaultClient(TuyaAPIConstant.tuyaHost, API.TUYA);
		Params params = new Params();
		params.setParam("page_no", "1"); 
		params.setParam("page_size", "999");
		 
		JSONObject result = client.send("/v1.0/users/ay15613571068820m89V/devices", params, Method.GET);
		System.out.println(result);
	}//16520638b4e62d5dd180
	
	
	@Test
	public void searchDeviceStatues() {
		Client client = new DefaultClient(TuyaAPIConstant.tuyaHost, API.TUYA);
		Params params = new Params();

		 
		JSONObject result = client.send("/v1.0/devices/16520638b4e62d5dd180/status", params, Method.GET);
		System.out.println(result);
	}
	
	@Test
	public void searchDeviceFunction() {
		Client client = new DefaultClient(TuyaAPIConstant.tuyaHost, API.TUYA);
		Params params = new Params();

		 
		JSONObject result = client.send("/v1.0/devices/16520638b4e62d5dd180/functions", params, Method.GET);
		System.out.println(result);
	}
	
	@Test
	public void searchUser() {
		Client client = new DefaultClient(TuyaAPIConstant.tuyaHost, API.TUYA);
		Params params = new Params();
		params.setParam("page_no", "1"); 
		params.setParam("page_size", "999");
		 
		JSONObject result = client.send("v1.0/apps/vlinsmart/users", params, Method.GET);
		System.out.println(result);//ay1553517463686g0eyA/v1.0/users/{uid}/devices
		///v1.0/apps/vlinsmart/users
	}

}
